/*
 * Knowledge
 */

import Knowledge from 'constants/knowledge';
import { CALL_BOOK_API } from '../middlewares';

/**
 * 创建知识点
 * @param data KnowledgeModal
 */
export function createKnowledge(data: Record<string, any>) {
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Knowledge.CREATE, Knowledge.CREATE_SUCCESS, Knowledge.CREATE_FAIL],
      uri: '/course-api/a/knowledge',
      method: 'POST',
      body: data,
    },
  };
}

/**
 * 更新知识点
 * @param data KnowledgeModal
 */
export function updateKnowledge(knowledgeId: number, data: Record<string, any>) {
  return {
    knowledgeId,
    [CALL_BOOK_API]: {
      types: [Knowledge.UPDATE, Knowledge.UPDATE_SUCCESS, Knowledge.UPDATE_FAIL],
      uri: `/course-api/a/knowledge/${knowledgeId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * 查询知识点详情，返回知识点所有数据
 * @param data KnowledgeModal
 */
export function fetchKnowledgeDetail(knowledgeId: number) {
  return {
    knowledgeId,
    [CALL_BOOK_API]: {
      types: [Knowledge.FETCH, Knowledge.FETCH_SUCCESS, Knowledge.FETCH_FAIL],
      uri: `/course-api/a/knowledge/${knowledgeId}`,
      method: 'GET',
    },
  };
}

/**
 * 删除知识点
 * @param knowledgeId
 */
export function removeKnowledge(knowledgeId: number) {
  return {
    knowledgeId,
    [CALL_BOOK_API]: {
      types: [Knowledge.REMOVE, Knowledge.REMOVE_SUCCESS, Knowledge.REMOVE_FAIL],
      uri: `/course-api/a/knowledge/${knowledgeId}`,
      method: 'DELETE',
    },
  };
}

/**
 * 查询知识点列表
 * @param filters 学期查询过滤
 */
export function fetchKnowledgeList(filters: Record<string, any> = {}) {
  return {
    filters,
    [CALL_BOOK_API]: {
      types: [Knowledge.FETCH_LIST, Knowledge.FETCH_LIST_SUCCESS, Knowledge.FETCH_LIST_FAIL],
      uri: '/course-api/a/knowledge',
      method: 'GET',
      body: filters,
    },
  };
}

/**
 * 查询已关联(绘本页)到Lesson的知识点列表
 * @param filters 学期查询过滤
 */
export function fetchLessonKnowledgeList(lessonId: number, filters: Record<string, any> = {}) {
  return {
    filters,
    [CALL_BOOK_API]: {
      types: [Knowledge.FETCH_LIST, Knowledge.FETCH_LIST_SUCCESS, Knowledge.FETCH_LIST_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/knowledge`,
      method: 'GET',
      body: filters,
    },
  };
}

/**
 * 知识点关联Tag
 * 清空的话，传空body
 * @param knowledgeId 知识点ID
 * @param tagIds 标签ID数组
 */
export function saveKnowledgeTags(knowledgeId: number, tagIds: number[]) {
  let body = {};
  if (tagIds.length > 0) {
    body = { tagIds };
  }
  return {
    knowledgeId,
    [CALL_BOOK_API]: {
      types: [Knowledge.SAVE_TAG, Knowledge.SAVE_TAG_SUCCESS, Knowledge.SAVE_TAG_FAIL],
      uri: `/course-api/a/knowledge/${knowledgeId}/tags`,
      method: 'PUT',
      body,
    },
  };
}

/**
 * 更新知识点附件
 * @param knowledgeId 知识点ID
 * @param images [{id, url, isFront }]
 */
export function saveKnowledgeImages(knowledgeId: number, images: object[]) {
  let body = {};
  if (images.length > 0) {
    body = { images };
  }
  return {
    knowledgeId,
    [CALL_BOOK_API]: {
      types: [Knowledge.SAVE_IMAGE, Knowledge.SAVE_IMAGE_SUCCESS, Knowledge.SAVE_IMAGE_FAIL],
      uri: `/course-api/a/knowledge/${knowledgeId}/images`,
      method: 'PUT',
      body,
    },
  };
}

/**
 * 更新知识点音频
 * @param knowledgeId 知识点ID
 * @param audio 音频
 */
export function saveKnowledgeAudio(knowledgeId: number, audioId: number) {
  let body = {};
  if (audioId) {
    body = { audioId };
  }
  return {
    knowledgeId,
    [CALL_BOOK_API]: {
      types: [Knowledge.SAVE_AUDIO, Knowledge.SAVE_AUDIO_SUCCESS, Knowledge.SAVE_AUDIO_FAIL],
      uri: `/course-api/a/knowledge/${knowledgeId}/audio`,
      method: 'PUT',
      body,
    },
  };
}
